import json
import socket

if __name__ == '__main__':
    MaxBytes = 1024 * 1024
    host = '127.0.0.1'
    port = 6789
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.settimeout(30)
    client.connect((host, port))

    data = {
        "header": {
            "device": "web",
            "token": "11111111111",
            "proto": "user_login"
        },
        "data": {
            "user_id": 1,
            "nickname": "test"
        },
        "send_time": ""
    }
    client.send(bytes(json.dumps(data), encoding="utf-8"))
    while True:
        inputData = input()
        if (inputData == "quit"):
            print("我要退出了，再见")
        break
        sendBytes = client.send(inputData.encode())
        if sendBytes <= 0:
            break
        recvData = client.recv(MaxBytes)
        if not recvData:
            print('接收数据为空，我要退出了')
        print(localTime, ' 接收到数据字节数:', len(recvData))
        print(recvData.decode())
        client.close()
